/**
 * 
 */
package com.sunnos.express.service;

import java.util.List;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunnos.express.entity.Right;
import com.sunnos.express.entity.Role;
import com.sunnos.express.entity.User;
import com.sunnos.express.mapper.RoleMapper;

/**
 * @author lf
 *
 */
@Service
public class RoleService {
	@Autowired
	private RoleMapper roleMapper;
	
	/**
	 * ��ȡ��ɫ
	 * @return
	 */
	public Role getRole(Map<String,Object> param) {
		return roleMapper.getRole(param);
	}
	
	/**
	 * ��ȡһ��Ȩ��
	 * @return
	 */
	public List<Right> getTopRighs() {
		return roleMapper.getTopRight();
	}
	
	/**
	 * ��ȡ����Ȩ��
	 * @return
	 */
	public List<Right> getSubRighs(int param) {
		return roleMapper.getSubRight(param);
	}
	
	/**
	 * ��ȡ��ɫ�б�
	 * @return
	 */
	public List<Role> getRoleList(Map<String,Object> param) {
		return roleMapper.getRoleList(param);
	}
	
	/**
	 * ��ȡָ����ɫ�û��б�
	 * @return
	 */
	public List<User> getRoleUserList(Map<String,Object> param) {
		return roleMapper.getRoleUserList(param);
	}
	
	public int getRoleListTotal() {
		return roleMapper.getRoleListTotal();
	}

	/**
	 * �����ɫ
	 * @param user
	 * @param param map-rights-rolename 
	 */
	@Transactional
	public void insertRole(Map<String,Object> param) {
		//�����ɫ
		roleMapper.insertRole((String)param.get("name"));		
//		String menuList = (String)param.get("rights");
//		if(!"".equals(menuList)) {
//			//�����ɫ�˵���j
//			param.put("role_id", this.getRole(param).getId());
//			this.insertRoleRight(param);
//		}
	}
	/**
	 * �༭��ɫ
	 * @param user
	 * @param param
	 */
	@Transactional
	public void updateRole(Map<String,Object> param) {
		//�༭��ɫ
		roleMapper.updateRole(param); //map need name,id==role_id
		//ɾ��ԭ�еĽ�ɫ�˵���j
		roleMapper.deleteRoleRight(Integer.parseInt(param.get("id").toString()));
		//�����ɫ�˵���j
		param.put("role_id", param.get("id"));
		this.insertRoleRight(param);//map need role_id,rights
	}
	/**
	 * ɾ���ɫ
	 * @param param
	 */
	@Transactional
	public void deleteRole(int roleId) {		
		roleMapper.deleteRole(roleId);
		roleMapper.deleteRoleRight(roleId);
	}
	/**
	 * ��ɫ�˵���j
	 * @param param
	 */
	public void insertRoleRight(Map<String,Object> param) {
		String menuList = (String)param.get("rights");
		String[] menuArray = menuList.split(",");
		for(int i=0;i<menuArray.length;i++) {
			param.put("right_id", menuArray[i]);
			roleMapper.insertRoleRight(param);
		}
	}
}
